package com.vnp.vchat.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.vnp.vchat.MainPanelActivity;
import com.vnp.vchat.R;
import com.vnp.vchat.UIControls.MyCircleImageView;
import com.vnp.vchat.bean.ObjKhachhang;
import com.vnp.vchat.util.Utils;

public class KhachhangAdapter extends ArrayAdapter<ObjKhachhang>{

	Context context;
	private List<ObjKhachhang> worldpopulationlist = null;
	ArrayList<ObjKhachhang> objKhachhangs = new ArrayList<ObjKhachhang>() ;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
								.cacheInMemory(false)
								.cacheOnDisc(true)
								.showImageForEmptyUri(R.drawable.defaul_avatar)
								.showImageOnFail(R.drawable.defaul_avatar)
								.bitmapConfig(Config.RGB_565)
								.displayer(new FadeInBitmapDisplayer(300, true, false, false))
								.build();
	
	HashMap<ObjKhachhang, Integer> mIdMap = new HashMap<ObjKhachhang, Integer>();
	int mCounter;
	
	
	public KhachhangAdapter(Context context, List<ObjKhachhang> worldpopulationlist) {
		super(context, R.layout.item_khachhang, worldpopulationlist);
		this.context = context;
		this.worldpopulationlist = worldpopulationlist;
		this.objKhachhangs.addAll(worldpopulationlist);
	}

	@Override
	public int getCount() {
		return worldpopulationlist.size();
	}

	@Override
	public ObjKhachhang getItem(int position) {
		return worldpopulationlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int position, View v, ViewGroup parent) {
		Holder holder = null;
		if (v==null){
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.item_khachhang, parent, false);
			holder = new Holder();

			
			holder.imgAvatar = (MyCircleImageView) v.findViewById(R.id.img_avatar);
			holder.txtName = (TextView) v.findViewById(R.id.txt_ten);
			holder.address = (TextView) v.findViewById(R.id.txt_address);
			holder.txtListDs = (TextView) v.findViewById(R.id.txt_danhsach);
			holder.txtWebsite = (TextView) v.findViewById(R.id.txt_website);
			holder.btnInfo = (ImageView) v.findViewById(R.id.btn_info);
			
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}
		
		// set font
		holder.txtName.setTypeface(Utils.getTypefaceBold(context), Typeface.BOLD);
		holder.address.setTypeface(Utils.getTypefaceLight(context), Typeface.BOLD);
		holder.txtListDs.setTypeface(Utils.getTypefaceRegular(context), Typeface.BOLD);
		holder.txtWebsite.setTypeface(Utils.getTypefaceMedium(context), Typeface.BOLD);
		
		final ObjKhachhang objKhachhang = worldpopulationlist.get(position);
		
		ImageLoader.getInstance().displayImage(objKhachhang.getUrlAvatar(), holder.imgAvatar, options);
		if (objKhachhang.getName().length() > 17){
			holder.txtName.setText(objKhachhang.getName().substring(0, 15)+"...");
		} else {
			holder.txtName.setText(objKhachhang.getName());
		}
		
		holder.address.setText(objKhachhang.getAddress());
		holder.txtListDs.setText(objKhachhang.getDsDaxem());
		if (objKhachhang.getTxtWebsite().length() > 12){
			holder.txtWebsite.setText("("+objKhachhang.getTxtWebsite().substring(0, 9)+"...)");
		} else {
			holder.txtWebsite.setText("("+objKhachhang.getTxtWebsite()+")");
		}
		
		holder.btnInfo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = objKhachhang.getUrlDangxem();
				if (!url.equals("")){
					MainPanelActivity.onGoto = true;
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
							.parse(url));
					context.startActivity(browserIntent);
				} else {
					
				}
			}
		});
		
		return v;
	}
	
	class Holder{
		MyCircleImageView imgAvatar;
		TextView txtName;
		TextView address;
		TextView txtListDs;
		TextView txtWebsite;
		ImageView btnInfo;
		
	}
	
	public void filter(String charText){
		charText = charText.toLowerCase(Locale.getDefault());
		worldpopulationlist.clear();
        if (charText.length() == 0) {
            worldpopulationlist.addAll(objKhachhangs);
        }
        else
        {
            for (ObjKhachhang objKhachhang : objKhachhangs)
            {
                if (objKhachhang.getName().toLowerCase(Locale.getDefault()).contains(charText))
                {
                    worldpopulationlist.add(objKhachhang);
                }
            }
        }
        notifyDataSetChanged();
		
	}
	
	public void addStableIdForDataAtPosition(int position) {
        mIdMap.put(objKhachhangs.get(position), ++mCounter);
    }

}
